﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Visuals;
using System.Drawing;
using Silvermoon.OpenGL.Native;
using Silvermoon.OpenGL;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Control to present text in one ore more lines, optionally with word wrap and custom alignments and drop shadow.
    /// </summary>
    public class Label : Control
    {
        private StringVisual stringVisual;
        private bool shadowed;

        #region ctor/dtor

        public Label()
            : base()
        {
            stringVisual = new StringVisual { LineMode = LineMode.MultiLine };
            LineAlignment = Alignment.Center;
            LineMode = LineMode.SingleLineEndElipsis;
        }

        #endregion
        #region properties

        /// <summary>
        /// Gets or sets whether to render with a shadow.
        /// </summary>
        public bool Shadowed
        {
            get { return shadowed; }
            set
            {
                if (shadowed != value)
                {
                    shadowed = value;
                    Invalidate(InvalidationFlag.Appearance);
                }
            }
        }

        public Alignment LineAlignment { get { return stringVisual.LineAlignment; } set { stringVisual.LineAlignment = value; } }

        public Alignment Alignment { get { return stringVisual.Alignment; } set { stringVisual.Alignment = value; } }

        public LineMode LineMode { get { return stringVisual.LineMode; } set { stringVisual.LineMode = value; } }

        public override IEnumerable<Visual> Visuals
        {
            get
            {
                yield return stringVisual;
            }
        }

        public string Text { get { return stringVisual.Text; } set { if (stringVisual.Text != value) { stringVisual.Text = value; Invalidate(InvalidationFlag.Text); } } }
        public FontSprite Font { get { return stringVisual.Font; } set { if (stringVisual.Font != value) { stringVisual.Font = value; Invalidate(InvalidationFlag.LayoutAppearance); } } }

        public Size TextSize { get { return Control.Unscale(stringVisual.TextSize); } }

        /// <summary>
        /// Gets or sets the <see cref="FontSpite"/> to use as reference to render each character refered to the specified <see cref="FontSprite"/>.
        /// </summary>
        public float ScaleFactor { get { return stringVisual.ScaleFactor; } set { if (stringVisual.ScaleFactor != value) { stringVisual.ScaleFactor = value; Invalidate(InvalidationFlag.LayoutAppearance); } } }

        #endregion
        #region Methods


        protected override void OnLayout()
        {
            stringVisual.Shadowed = Shadowed;
            if (CheckFlag(InvalidationFlag.Size | InvalidationFlag.Margin))
            {
                stringVisual.Size = Scale(Size);
            }
            base.OnLayout();
        }

        protected override void OnSizeChanged()
        {
            base.OnSizeChanged();
        }

        protected override void OnTextColorChanged()
        {
            stringVisual.Color = TextColor;
        }

        internal void SetTextColor(Color color)
        {
            stringVisual.Color = color;
        }

        #endregion


    }
}
